<?php
/* +----------------------------------------------------------------------
// | 	xaoam.com
// |	author:am
// |	gitee:https://gitee.com/xaoam/amAdmin
// |	email:hi@xaoam.com
// |	
// +----------------------------------------------------------------------*/
namespace app\admin\controller;
use think\Controller;
use think\Db;

class Tag extends Base {

    public function initialize(){
        parent::initialize();
		$this->model = new \app\admin\model\TagM;
		
    }

    public function index(){
        return $this->fetch();
    }
	public function lists(){
		$d = request()->param();
		$type = isset($d['type'])?$d['type']:1;
		$arr =[];
		$res = Db::name('tag')->where('type',$type)->select();
			foreach($res as $key=>$row){
				$arr[$key] = $row;
				if(isset($d['id'])){
					if($row['id'] == $d['id'] || $row['parentId'] == $d['id']){
						$arr[$key]['disabled'] = true; 
					}
				}

			}
			$this->reJson['code'] = 200;
			$this->reJson['data'] = $arr;
				if($type == 1){
					$this->reJson['msg'] = '文章标签';
				}else{
					$this->reJson['msg'] = '商品标签';			
				}
		return json($this->reJson);	
	}
	public function add(){
		$data = request()->param();
		if(request()->isPost()){
			$res = $this->model->add($data);
			if($res){
				$this->reJson['msg']='增加成功';
				$this->reJson['code']=200;
			}
			return json($this->reJson);			
		}else{
			return $this->fetch();			
		}
	}
	public function edit(){
		$data = request()->param();
		if(request()->isPost()){
			$res = $this->model->edit($data);
			if($res){
				$this->reJson['msg']='增加成功';
				$this->reJson['code']=200;
			}
			return json($this->reJson);
		}else{
			$this->assign('d_json',json_encode(Db::name('tag')->where('id',$data['id'])->find()));
			return $this->fetch();			
		}

	}
	public function del(){
		$data = request()->param();
		$res = $this->model->find(['parentId'=>$data['id']]);
		if($res){
			$this->reJson['msg']='删除标签包含子标签,请删除子标签';
			$this->reJson['code']=0;			
		}else{
			$res = $this->model->del($data);
			if($res){
				$this->reJson['msg']='删除成功';
				$this->reJson['code']=200;
			}
		}
		return json($this->reJson);
	}
}
